package org.flexthinker.binding.validation.support
{
    import mx.validators.Validator;

    import org.flexthinker.binding.form.support.DefaultValidatingFormModel;
    import org.flexthinker.binding.value.support.MockBean;
    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;
    import org.hamcrest.object.nullValue;

    public class RulesValidatorTest
    {
        private var rulesValidator:RulesValidator;

        private var formModel:DefaultValidatingFormModel;

        private var mockBean:MockBean;

        private var validators:Vector.<Validator> = new Vector.<Validator>();


        public function RulesValidatorTest()
        {
        }

        [Before]
        public function setUp():void
        {
            mockBean = new MockBean();

            var v:Validator = new Validator();
            v.enabled = true;
            v.required = true;
            v.property = "simpleString";
            validators.push( v );
            rulesValidator = new RulesValidator(validators);

            formModel = new DefaultValidatingFormModel( mockBean );
            formModel.setValidator( rulesValidator );
        }

        [Test]
        public function testInitialization():void
        {
            assertThat("FormModel is wrong", rulesValidator.getFormModel(), equalTo(formModel));
        }


        [Test]
        public function testValidationOnNullValues():void
        {
            mockBean.simpleString = "initial";

            assertThat("RulesValidator should have no error", rulesValidator.validateProperty("simpleString").getHasErrors(), equalTo(false));
            formModel.getValueModel("simpleString").setValue(null);

            assertThat("FormObject's property shouldn't be changed", mockBean.simpleString, equalTo("initial"));
            assertThat("FormModel's property should be changed ", formModel.getValueModel("simpleString").getValue(), nullValue());

            assertThat("RulesValidator should return error", rulesValidator.validateProperty("simpleString").getHasErrors(), equalTo(true));
        }
    }
}
